package springMvc;

import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import entity.User;

@SessionAttributes(value = { "CURR_USER", "uname" }, types = { User.class })
@Controller
public class SpringController {
	/*
	 * @RequestMapping("/login") public String SpringController(User user,
	 * HttpSession session) {
	 * System.out.println("=========执行登录测试====================");
	 * session.setAttribute("CURR_USER", user); return "success"; }
	 */
	// map测试需要添加@SessionAttributes(value={"CURR_USER","uname"},types={User.class})
	/*
	 * @RequestMapping("/login") public String SpringController(User
	 * user,Map<String, Object> map) {
	 * System.out.println("=========执行登录测试====================");
	 * map.put("CURR_USER", user); return "success"; }
	 */

	// ModelMap测试
	
	  @RequestMapping("/login") 
	  
	  public String SpringController(User user,ModelMap map) {
	  System.out.println("=========执行登录测试====================");
	  map.put("CURR_USER", user); return "success"; }
	 
	@RequestMapping("/Cookie")
	public String Cookie(User user, HttpServletResponse response) {
		System.out.println("================Cookie方法=========================");
		Cookie cookie = new Cookie("c1", "tom");
		Cookie cookie1 = new Cookie("c2", "jick");
		cookie.setMaxAge(60 * 60 * 3);
		cookie1.setMaxAge(60 * 60 * 3);
		cookie.setPath("/");
		cookie1.setPath("/");
		response.addCookie(cookie);
		response.addCookie(cookie1);
		return "success";

	}

	/*@RequestMapping("/getCookie")
	public String getCookie(User user, HttpServletRequest request, HttpServletResponse response) {
		System.out.println("================Cookie方法=========================");
		Cookie[] cookies = request.getCookies();
		for (Cookie cookie : cookies) {
			System.out.println(cookie.getName() + ",.........." + cookie.getValue());
		}
		for (Cookie cookie : cookies) {
			if (cookie.getName().equals("c1")) {
				cookie.setMaxAge(0);
				cookie.setPath("/");
				response.addCookie(cookie);
			}
			//注销记录Cookie,Cookie进行 session 跟踪而储存在用户本地终端上的数据
		}
		return "success";
	}
*/
//避免不存在的记录session报错设置required=false
/*@RequestMapping("/getCookie")
public String getCookie(@CookieValue(value="c1",required=false) String c,@CookieValue(value="JSESSIONID")String sesssionId) {
	System.out.println("================Cookie方法=========================");
System.out.println("获取到name为c1的cookie value值为"+c);
System.out.println("session"+sesssionId);
	return "success";
}*/
//@ModelAttribute  注解
@ModelAttribute("MyUser")
public User pre(String uname,String pwd){
	System.out.println("=============执行pre方法======================");
	User user=new User();
	user.setUname(uname);
	user.setPwd(pwd);
	return user;
}
@RequestMapping("/login")
public String userLogin(ModelMap modelMap){
	System.out.println("=============执行登录方法======================");
	System.out.println(modelMap.get("MyUser"));
	return "success";
}
//form四种表单请求
//查询
@RequestMapping(value="/user",method=RequestMethod.GET)
public String select(String uname){
	System.out.println("===========执行查询方法==============");
	System.out.println("获取查询方法的名字"+uname);
	return "success";
}
//添加
@RequestMapping(value="/user",method=RequestMethod.POST)
public String add(String uname){
	System.out.println("===========执行添加方法==============");
	System.out.println("获取添加方法的名字"+uname);
	return "success";
}
//删除
@RequestMapping(value="/user",method=RequestMethod.DELETE)
public String deleate(String uname){
	System.out.println("===========执行删除方法==============");
	System.out.println("获取删除方法的名字"+uname);
	return "success";
}
//修改
@RequestMapping(value="/user",method=RequestMethod.PUT)
public String update(String uname){
	System.out.println("===========执行修改方法==============");
	System.out.println("获取修改方法的名字"+uname);
	return "success";
}




}
